package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLocationApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private UserLocationApi userLocationApi;


    // 查询用户列表
    public PageResult findList(Integer pageNumber, Integer pageSize) {

        // 查询
        IPage<UserInfo> iPage = userInfoApi.selectAll(pageNumber, pageSize);

        // 获取用户数量
        long total = iPage.getTotal();

        // 获取用户列表
        List<UserInfo> userInfoList = iPage.getRecords();

        // 判断
        if (userInfoList == null || userInfoList.size() <= 0){

            return new PageResult();
        }

        // 转化为VO对象
        List<UserInfoVo> vos = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {

            // 创建VO对象
            UserInfoVo userInfoVo = new UserInfoVo();

            BeanUtils.copyProperties(userInfo,userInfoVo);

            vos.add(userInfoVo);
        }
        return new PageResult(pageNumber,pageSize,total,vos);

    }

    // 查询用户坐标
    public String selectLocation(Long userId) {

        // 查询
        String location = userLocationApi.selectLocation(userId);


        return location;
    }
}
